package com.budwk.app.sms.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sms.dto.SmsMsgDTO;
import com.budwk.app.sms.service.ZtSmsService;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.*;

/**
 * @author ZY.Wang
 * @since 2024/5/29
 */
@IocBean
@Slf4j
public class ZtSmsServiceImpl implements ZtSmsService {

    public static final String notSendUsernameWordFilter = "测1";

    @Inject
    private PropertiesProxy propertiesProxy;

    @Inject
    private SysUserService sysUserService;

    public Integer ztSendSafetyCheckInfoMsg(String phone, String url) {
        SmsMsgDTO smsMsgDTO = new SmsMsgDTO();
        smsMsgDTO.setTempId("145140");
        smsMsgDTO.setPhoneNumberSet(Sets.newHashSet(phone));

        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.put("url", url);

        smsMsgDTO.setTemplateParamMap(paramMap);
        return ztSendMessageBatch(smsMsgDTO);
    }

    @Override
    public Integer ztSendMessageBatch(SmsMsgDTO smsMsgDTO) {

        String templateUrl = smsMsgDTO.getTemplateParamMap().get("url").replace("https://wxaurl.cn/", "");
        smsMsgDTO.getTemplateParamMap().put("url", templateUrl);

        try {
            String enable = propertiesProxy.get("opa.sms.enable");
            if (enable.equals("false")) return 0;

            String userName = "Dfsjkj";
            String tKey = (new Date().getTime() + "").substring(0, 10);
            String password = DigestUtil.md5Hex(DigestUtil.md5Hex("Dfsjkjjkmm123") + tKey);
            String signature = "【祥云小助】";
            String tpId = smsMsgDTO.getTempId();
            List<Map<String, Object>> records = Lists.newArrayList();

            for (String phone : smsMsgDTO.getPhoneNumberSet()) {

                try {
                    // wzy 2024-5-30 如果电话号码在系统中存在，并且名称包含测1就不发送信息
                    Sys_user sysUser = sysUserService.getUserByLoginname(phone);
                    if (sysUser != null) {
                        String username = Optional.ofNullable(sysUser.getUsername()).orElse("");
                        if (username.contains(notSendUsernameWordFilter)) continue;
                    }
                } catch (Exception ignore) {
                }

                Map<String, Object> mobileParam = Maps.newHashMap();
                mobileParam.put("mobile", phone);

                Map<String, String> tpContent = Maps.newHashMap();
                Map<String, String> templateParamMap = smsMsgDTO.getTemplateParamMap();
                for (String paramKey : templateParamMap.keySet()) {
                    tpContent.put(paramKey, templateParamMap.get(paramKey));
                }
                mobileParam.put("tpContent", tpContent);
                records.add(mobileParam);
            }

            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("username", userName);
            paramMap.put("password", password);
            paramMap.put("tKey", tKey);
            paramMap.put("tpId", tpId);
            paramMap.put("signature", signature);
            paramMap.put("records", records);
            String requestBodyStr = JSONObject.toJSONString(paramMap);
            String resStr = HttpUtil.post("https://api-shss.zthysms.com/v2/sendSmsTp", requestBodyStr);

            JSONObject resJson = JSONObject.parseObject(resStr);
            Integer code = resJson.getInteger("code");

            if (code == 200) return 1;

        } catch (Exception e) {
            e.printStackTrace();
            log.error("zt sms send message error!");
        }

        return 0;
    }
}
